package network;

/*
 *  author:  Iringo Czakany
 */

import java.net.*;

import java.io.*; 
public class Connect {

	 public final static int HTTP_PORT=80;
	 InetAddress wwwserver; 
	 DataInputStream FinalReader; 
	 PrintStream FinalPrinter; 
	 String response;
	/*Constructor is called whenever a new object of class is created, it gets the name of the server of the machine*/
	 public Connect(String Server) throws UnknownHostException
	  { 
	   wwwserver=InetAddress.getByName(Server); 
	  } 
	/*Gets the html file from the servers*/
	 @SuppressWarnings({ "deprecation", "resource"})
	public InputStream get(String filename) throws IOException
	  { 
	   Socket socket; 
	   InputStream InitialReader; 
	   OutputStream InitialWriter; 
	   socket= new Socket(wwwserver,HTTP_PORT); 
	   InitialReader=socket.getInputStream(); 
	   InitialWriter=socket.getOutputStream(); 
	   FinalReader=new DataInputStream(InitialReader); 
	   FinalPrinter=new PrintStream(InitialWriter); 
	    if(InitialReader==null || InitialWriter==null) 
	     { 
	      return null; 
	     } 
	   FinalPrinter.println("GET " + filename + " HTTP/1.0\n"); 
	    while((response=FinalReader.readLine()).length() > 0) 
	     { 
	      if(response.startsWith("HTTP/1.0 404 Not Found")) 
	       {
	    	  return null;  
	       }
	     }  
	   return FinalReader; 
	  } 
	} 


